package com.ngocvo.QuanLyThuChi;

import java.text.ParseException;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
/*	
 * Class name	: ThuActivity
 * Create Date	: 08-03-2011
 * Author		: Ngoc Vo
 * Description	: Extends Activity, the activity for inserting 
 * 				  a new CashFlow object with FlowType = 'Thu'
 * Modified by	: Ngoc Vo
 * Modified Date: 08-04-2011
 * Modify log	: Add comments, logs
 * 
 */
public class ThuActivity extends Activity{
/*
 * ================================================================================================
 * 											VARIABLES SECTION
 * ================================================================================================
 */
	private static final int HOME = Menu.FIRST;
 
/*
 * ================================================================================================
 * 										METHODS SECTION
 * ================================================================================================
 */

/*
 * ID			: 01
 * Method name	: onCreateOptionsMenu
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: Menu menu
 * Input		: menu
 * Output		: boolean
 * Description	: Create options menu item: 'Home'
 * Modify date	:
 * Modify log	:
 */
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, HOME, 0,"Home" ).setIcon(android.R.drawable.ic_menu_info_details);
		return true;
	}
/*
 * ID			: 02
 * Method name	: onOptionsItemSelected
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: android.view.MenuItem item
 * Input		: Menu item
 * Output		: boolean
 * Description	: Move to main screen when user click on item 'Home'
 * 				  in options menu
 * Modify date	:
 * Modify log	:
 */
	public boolean onOptionsItemSelected(android.view.MenuItem item) {
		switch (item.getItemId()) {
		case HOME:
			Intent i = new Intent(ThuActivity.this, StartActivity.class);
			startActivity(i);
			break;
		}
		return true;
	}
/*
 * ID			: 03
 * Method name	: onCreate
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: Bundle savedInstanceState
 * Input		: savedInstanceState
 * Output		: None
 * Description	: Setup views and event listeners for the activity 				  
 * Modify date	:
 * Modify log	:
 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thu_layout);
		
		//Set flow type
		Global.flowType = FlowType.Thu;
		
		//Get the date, time display
		final TextView txt_DateDisplay = (TextView)findViewById(R.id.txt_NgayDisplay);
		final TextView txt_TimeDisplay = (TextView)findViewById(R.id.txt_CustClock);
		final Button btn_Save = (Button)findViewById(R.id.btn_Luu);
		final Button btn_SaveCont = (Button)findViewById(R.id.btn_LuuTiep);
		final EditText edit_Amount = (EditText)findViewById(R.id.edit_SoTien);
		final EditText edit_Name = (EditText)findViewById(R.id.edit_GhiChu);
		
		//get current datetime and display on the TextViews
		Global.updateCurrentDate(txt_DateDisplay, txt_TimeDisplay);
		
		//Set create date for the CashFlow object
		Calendar c = Calendar.getInstance();		
		Global.strCreateDate = String.valueOf(c.get(Calendar.YEAR)) + "-" +
								String.valueOf(Global.pad(c.get(Calendar.MONTH)+1)) + "-" +
								String.valueOf(Global.pad(c.get(Calendar.DAY_OF_MONTH))) + " " +
								String.valueOf(Global.pad(c.get(Calendar.HOUR_OF_DAY))) + ":" +
								String.valueOf(Global.pad(c.get(Calendar.MINUTE))) + ":" +
								String.valueOf(Global.pad(c.get(Calendar.SECOND)));
		
		//Show date picker dialog when user click on txt_DateDisplay
		txt_DateDisplay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(Global.DATE_DIALOG_ID);				
			}
		});
		
		//Show time picker dialog when user click on txt_TimeDisplay
		txt_TimeDisplay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(Global.TIME_DIALOG_ID);				
			}
		});
		
		/*
		 * Create a CashFlow object with the information that user provides
		 * and save the object into database
		 * then move back to main screen
		 */
		btn_Save.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				
				//Get value of amount and name
				String Amount = edit_Amount.getText().toString();
				String Name = edit_Name.getText().toString();							
				String FlowBackDate = "";	//Set default flow back date = empty
				Global.srtStatus = -1;	// Set default status = -1
				
				//Create CashFlow object and save in database
				try {
					Global.SaveCashFlow(ThuActivity.this, Global.flowType, edit_Amount, Amount, Name, FlowBackDate);
				} catch (ParseException e) {
					AlertDialog.Builder builder = new AlertDialog.Builder(ThuActivity.this);
					builder.setTitle("Error orrcurs!");
					builder.setMessage("The error is: " + e.getMessage());
					builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub								
						}
					});
					builder.show();
				}
				//Move back to main screen
				Intent i = new Intent(ThuActivity.this, StartActivity.class);
				startActivity(i);
			}
		});
		
		/*
		 * Create a CashFlow object with the information that user provides
		 * and save the object into database
		 * then clear the form and stay at the form
		 */
		btn_SaveCont.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//Get value of amount and name
				String Amount = edit_Amount.getText().toString();
				String Name = edit_Name.getText().toString();
				String FlowBackDate = "";	//Set default flow back date = empty
				Global.srtStatus = -1;	// Set default status = -1
				
				//Create CashFlow object and save in database		
				try {
					Global.SaveCashFlow(ThuActivity.this, Global.flowType, edit_Amount, Amount, Name, FlowBackDate);
				} catch (ParseException e) {
					AlertDialog.Builder builder = new AlertDialog.Builder(ThuActivity.this);
					builder.setTitle("Error orrcurs!");
					builder.setMessage("The error is: " + e.getMessage());
					builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub								
						}
					});
					builder.show();
				}
				//Clear form data and update current datetime
				edit_Amount.setText("");
				edit_Name.setText("");
				Global.updateCurrentDate(txt_DateDisplay, txt_TimeDisplay);
			}
		});
	}
/*
 * ID			: 04
 * Method name	: onCreateDialog
 * Create date	: 08-03-2011
 * Author		: Ngoc Vo
 * Parameter	: int id
 * Input		: dialog id
 * Output		: None
 * Description	: Create date picker and time picker dialogs 				  
 * Modify date	:
 * Modify log	:
 */
	@Override
	protected Dialog onCreateDialog(int id) {
		//Get the TextViews to update date, time when user set
		final TextView txt_DateDisplay = (TextView)findViewById(R.id.txt_NgayDisplay);
		final TextView txt_TimeDisplay = (TextView)findViewById(R.id.txt_CustClock);
		
		//Create listener when user set date
		DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				Global.intYear = year;
				Global.intMonth = monthOfYear + 1;
				Global.intDay = dayOfMonth;
				Global.updateDate(txt_DateDisplay);
			}
		};
		//Create listener when user set time
		TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
			
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				Global.intHour = hourOfDay;
				Global.intMinute = minute;				
				Global.updateTime(txt_TimeDisplay);
			}
		};
		//Create the dialogs
		switch(id){
			case Global.DATE_DIALOG_ID:
				return new DatePickerDialog(this, mDateSetListener, Global.intYear, Global.intMonth - 1, Global.intDay);
			case Global.TIME_DIALOG_ID:
				return new TimePickerDialog(this, mTimeSetListener, Global.intHour, Global.intMinute, true);
		}
		return null;
	}

}
